package ph.com.gs3.formalistics.model.valueobjects.application;

import java.util.ArrayList;
import java.util.List;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import ph.com.gs3.formalistics.utilities.FLLogger;

public class FormField {

	public static final String TAG = FormField.class.getSimpleName();

	public static enum FieldType {
		TEXT_FIELD, TEXT_AREA, DROPDOWN, CHECK_BOX_GROUP, RADIO_BUTTON_GROUP, UNSUPPORTED
	}

	private String id;
	private String label;
	private FieldType type;

	private String placeHolder;
	private String value;
	private List<String> options;
	private boolean enabled = true;

	public static FormField createFromJSON(JSONObject rawJSON) throws InvalidFormFieldException {

		FormField formField = new FormField();

		try {
			formField.id = rawJSON.getString("id");
			formField.label = rawJSON.getString("label");

			formField.setType(rawJSON.getString("type"));

			// optional fields
			if (rawJSON.has("value")) {
				formField.value = rawJSON.getString("value");
			}

			if (rawJSON.has("placeholder")) {
				formField.placeHolder = rawJSON.getString("placeholder");
			}

			if (rawJSON.has("options")) {
				JSONArray optionsJSON = new JSONArray(rawJSON.getString("options"));
				formField.options = new ArrayList<String>();

				for (int i = 0; i < optionsJSON.length(); i++) {
					formField.options.add(optionsJSON.getString(i));
				}
			}

		} catch (JSONException e) {
			throw new InvalidFormFieldException(rawJSON.toString(), e);
		}

		return formField;

	}

	// ========================================================================
	// Getters & Setters

	public String getId() {
		return id;
	}

	public String getLabel() {
		return label;
	}

	public FieldType getType() {
		return type;
	}

	public String getPlaceHolder() {
		return placeHolder;
	}

	public String getValue() {
		return value;
	}

	public boolean isEnabled() {
		return enabled;
	}

	public List<String> getOptions() {
		return options;
	}

	public void setEnabled(boolean enabled) {
		this.enabled = enabled;
	}

	private void setType(String type) {
		if ("TEXT_FIELD".equals(type)) {
			this.type = FieldType.TEXT_FIELD;
		} else if ("TEXT_AREA".equals(type)) {
			this.type = FieldType.TEXT_AREA;
		} else if ("DROPDOWN".equals(type)) {
			this.type = FieldType.DROPDOWN;
		} else if ("CHECK_BOX_GROUP".equals(type)) {
			this.type = FieldType.CHECK_BOX_GROUP;
		} else if ("RADIO_BUTTON_GROUP".equals(type)) {
			this.type = FieldType.RADIO_BUTTON_GROUP;
		} else {
			this.type = FieldType.UNSUPPORTED;
			FLLogger.d(TAG, "Unsupported field: " + type);
		}

	}

	// ========================================================================
	// Exceptions

	public static class InvalidFormFieldException extends Exception {

		private static final long serialVersionUID = 668103959494161980L;

		private String message;

		public InvalidFormFieldException(String rawJSONString, Exception e) {
			super(e);

			message = "Unable to create a field out of the raw JSON provided: " + rawJSONString;

		}

		@Override
		public String getMessage() {
			return message;
		}

	}

}
